package com.jxl.domain.handler.subject;

import com.jxl.common.enums.DeletedCodeEnum;
import com.jxl.common.enums.SubjectTypeEnum;
import com.jxl.domain.convert.SubjectAnswerConvert;
import com.jxl.domain.entity.SubjectAnswerBO;
import com.jxl.domain.entity.SubjectInfoBO;
import com.jxl.domain.entity.SubjectOptionBO;
import com.jxl.infra.basic.entity.SubjectMultiple;
import com.jxl.infra.basic.entity.SubjectRadio;
import com.jxl.infra.basic.service.SubjectMultipleService;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * 多选策略
 */
@Component
public class MultipleHandler implements SubjectTypeHandler{

    @Resource
    private SubjectMultipleService subjectMultipleService;


    @Override
    public SubjectTypeEnum getHandlerType() {
        return SubjectTypeEnum.MULTIPLE;
    }

    @Override
    public void add(SubjectInfoBO subjectInfoBO) {
        //进行类型的转换
        List<SubjectMultiple> subjectMultiples = new LinkedList<>();
        List<SubjectAnswerBO> optionList = subjectInfoBO.getOptionList();
        if (CollectionUtils.isEmpty(optionList)) return;
        List<SubjectMultiple> subjectMultiples1 =
                SubjectAnswerConvert.INSTANCE.convertBOToSubjectMutiple(optionList);
        subjectMultiples1.forEach(subjectMultiple -> {
            SubjectMultiple subjectMultiple1 = new SubjectMultiple();
            subjectMultiple1.setIsCorrect(subjectMultiple.getIsCorrect());
            subjectMultiple1.setOptionContent(subjectMultiple.getOptionContent());
            subjectMultiple1.setOptionType(subjectMultiple.getOptionType());
            subjectMultiple1.setIsDeleted(DeletedCodeEnum.UN_DELETE.getCode());
            subjectMultiple1.setSubjectId(subjectInfoBO.getSubjectId());
            subjectMultiples.add(subjectMultiple1);
        });
        subjectMultipleService.insertBatch(subjectMultiples);
    }

    @Override
    public SubjectOptionBO query(Long id) {
        SubjectMultiple subjectMultiple = new SubjectMultiple();
        subjectMultiple.setSubjectId(id);
        subjectMultiple.setIsDeleted(DeletedCodeEnum.UN_DELETE.getCode());
        List<SubjectMultiple> query = subjectMultipleService.query(subjectMultiple);
        SubjectOptionBO subjectOptionBO = new SubjectOptionBO();
        List<SubjectAnswerBO> list = new ArrayList<>();
        if (query!=null && query.size()!=0){
            query.forEach(subjectMultiple1->{
                SubjectAnswerBO subjectAnswerBO = new SubjectAnswerBO();
                subjectAnswerBO.setIsCorrect(subjectMultiple1.getIsCorrect());
                subjectAnswerBO.setOptionType(subjectMultiple1.getOptionType());
                subjectAnswerBO.setOptionContent(subjectMultiple1.getOptionContent());
                list.add(subjectAnswerBO);
            });
            subjectOptionBO.setOptionList(list);
        }
        return subjectOptionBO;
    }
}
